package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.LoginUser;
import com.sright.bcard.common.service.share.dto.request.EnterpriseCardRequest;
import com.sright.bcard.content.share.service.CardService;
import com.sright.bcard.enterprise.share.request.UpdateEnterpriseParam;
import com.sright.bcard.enterprise.share.service.EnterpriseService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业相关controller
 */
@RestController
@RequestMapping("/user")
public class EnterpriseController extends BaseController{

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private CardService cardService;

    /**
     * 获取企业信息
     * @return
     */
     @GetMapping("/enterpriseInfo.json")
     @EnableLoginAuth
     public SrightResponse getEnterpriseInfo() {
         LoginUser loginUser = new LoginUser();
         loginUser.setEnterpriseId(userContext.getEnterpriseId());
         loginUser.setUserId(userContext.getUserId());
         loginUser.setToken(userContext.getToken());
         return enterpriseService.getEnterprise(loginUser);
     }

    /**
     * 修改企业信息
     * @param param
     * @return
     */
     @PutMapping("/enterpriseInfo.json")
     @EnableLoginAuth
     public SrightResponse updateEnterpriseInfo(@Validated @RequestBody UpdateEnterpriseParam param) {
        param.setEnterpriseId(userContext.getEnterpriseId());
        param.setUserId(userContext.getUserId());
        param.setToken(userContext.getToken());
        return enterpriseService.updateEnterpriseInfo(param);
     }

    /**
     * 企业名片列表
     * @return
     */
    @GetMapping("/enterpriseCardList.json")
    @EnableLoginAuth
    public SrightResponse getEnterpriseCardList(EnterpriseCardRequest enterpriseCardRequest) {
        buildLogionUser(enterpriseCardRequest);
        return cardService.list(enterpriseCardRequest);
    }

    /**
     * 添加企业名片
     * @return
     */
    @PostMapping("/addEnterpriseCard.json")
    @EnableLoginAuth
    public SrightResponse addEnterpriseCard(@RequestBody EnterpriseCardRequest enterpriseCardRequest) {
        buildLogionUser(enterpriseCardRequest);
        return cardService.addEnterpriseCard(enterpriseCardRequest);
    }

    /**
     * 修改企业名片
     * @param enterpriseCardRequest
     * @return
     */
    @PostMapping("/editEnterpriseCard.json")
    @EnableLoginAuth
    public SrightResponse editEnterpriseCard(@RequestBody EnterpriseCardRequest enterpriseCardRequest) {
        buildLogionUser(enterpriseCardRequest);
        return cardService.editEnterpriseCard(enterpriseCardRequest);
    }

    /**
     * 企业名片详情
     * @param id
     * @return
     */
    @GetMapping("/enterpriseCardDetail.json")
    @EnableLoginAuth
    public SrightResponse enterpriseCardDetail(@RequestParam(value = "id") Long id) {
        EnterpriseCardRequest enterpriseCardRequest = new EnterpriseCardRequest();
        enterpriseCardRequest.setId(id);
        buildLogionUser(enterpriseCardRequest);
        return cardService.enterpriseCardDetail(enterpriseCardRequest);
    }

    /**
     * 删除企业名片
     * @return
     */
    @DeleteMapping("/deleteEnterpriseCard.json")
    @EnableLoginAuth
    public SrightResponse deleteEnterpriseCard(@RequestBody EnterpriseCardRequest enterpriseCardRequest) {
        buildLogionUser(enterpriseCardRequest);
        return cardService.deleteEnterpriseCard(enterpriseCardRequest);
    }

    /**
     * 修改企业名片标题
     * @return
     */
    @PutMapping("/updateEnterpriseCardName.json")
    @EnableLoginAuth
    public SrightResponse updateEnterpriseCardName(@RequestBody EnterpriseCardRequest enterpriseCardRequest) {
        buildLogionUser(enterpriseCardRequest);
        return cardService.updateEnterpriseCardName(enterpriseCardRequest);
    }

    private void buildLogionUser(EnterpriseCardRequest enterpriseCardRequest) {
        enterpriseCardRequest.setEnterpriseId(userContext.getEnterpriseId());
        enterpriseCardRequest.setUserId(userContext.getUserId());
        enterpriseCardRequest.setToken(enterpriseCardRequest.getToken());
    }

}
